package net.xxcxcxcx.xc_enhancedmob.skills.basics.individual.rare;

import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.DyeColor;
import net.minecraft.util.math.Quaternion;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.math.Vec3f;
import net.xxcxcxcx.xc_enhancedmob.config.skill.SkillConfig;
import net.xxcxcxcx.xc_enhancedmob.entity.CustomProjectileEntity;
import net.xxcxcxcx.xc_enhancedmob.entity.SaveFireworkRocketEntity;
import net.xxcxcxcx.xc_enhancedmob.skills.BaseSkill;
import net.xxcxcxcx.xc_enhancedmob.skills.SkillRarity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

public class RocketSkill extends BaseSkill {

    public RocketSkill(int level) {
        super(level);
    }

    public RocketSkill(NbtCompound nbtCompound) {
        super(nbtCompound);
    }

    public RocketSkill() {
    }

    @Override
    public SkillConfig getDefaultConfig() {
        SkillConfig skillConfig = super.getDefaultConfig();
        skillConfig.setCooldown(60);
        skillConfig.setMessage(getConfigMessage(skillConfig));
        return skillConfig;
    }

    @Override
    public String getConfigMessage(SkillConfig skillConfig) {
        if (skillConfig == null) return super.getConfigMessage(null);
        return String.format("火箭技能，可以发射火箭远程攻击。随等级提升伤害。冷却时间 %d tick", skillConfig.getCooldown());
    }

    public SkillRarity getSkillRarity() {
        return SkillRarity.Rare;
    }

    @Override
    public boolean canUseAbility(MobEntity mobEntity) {
        return (mobEntity.getTarget() != null && getSkilltime() < mobEntity.world.getTime());
    }

    public void shoot(LivingEntity entity, LivingEntity target, CustomProjectileEntity projectile, float multishotSpray, float speed) {
        CustomProjectileEntity entity2 = projectile;
        double d = target.getX() - entity.getX();
        double e = target.getZ() - entity.getZ();
        double g = target.getBodyY(0.666666666666) - entity2.getY();
        Vec3f vec3f = this.getProjectileLaunchVelocity(entity, new Vec3d(d, g, e), multishotSpray);
        projectile.setVelocity(vec3f.getX(), vec3f.getY(), vec3f.getZ(), speed, 14 - entity.world.getDifficulty().getId() * 4);
    }

    public Vec3f getProjectileLaunchVelocity(LivingEntity entity, Vec3d positionDelta, float multishotSpray) {
        Vec3d vec3d = positionDelta.normalize();
        Vec3d vec3d2 = vec3d.crossProduct(new Vec3d(0.0, 1.0, 0.0));
        if (vec3d2.lengthSquared() <= 1.0E-7) {
            vec3d2 = vec3d.crossProduct(entity.getOppositeRotationVector(1.0f));
        }
        Quaternion quaternion = new Quaternion(new Vec3f(vec3d2), 90.0f, true);
        Vec3f vec3f = new Vec3f(vec3d);
        vec3f.rotate(quaternion);
        Quaternion quaternion2 = new Quaternion(vec3f, multishotSpray, true);
        Vec3f vec3f2 = new Vec3f(vec3d);
        vec3f2.rotate(quaternion2);
        return vec3f2;
    }

    @Override
    public boolean onAbilityUse(MobEntity mobEntity) {
        if (canUseAbility(mobEntity)) {
            setSkilltime(mobEntity.world.getTime() + getCooldown());
            ItemStack itemStack = new ItemStack(Items.FIREWORK_ROCKET);
            Random random = new Random();
            NbtCompound fireworkstag = new NbtCompound();
            NbtCompound compoundtag = new NbtCompound();
            List<Integer> list = new ArrayList<>();
            list.add(DyeColor.byId(random.nextInt(15)).getFireworkColor());
            compoundtag.putIntArray("Colors", list);
            list = new ArrayList<>();
            list.add(DyeColor.byId(random.nextInt(15)).getFireworkColor());
            compoundtag.putIntArray("FadeColors", list);
            compoundtag.putBoolean("Trail", true);
            compoundtag.putBoolean("Flicker", true);
            compoundtag.putByte("Type", (byte) 1);
            fireworkstag.putByte("Flight", (byte) 10);
            NbtList listTag = new NbtList();
            listTag.add(compoundtag);
            fireworkstag.put("Explosions", listTag);
            itemStack.putSubTag("Fireworks", fireworkstag);
            SaveFireworkRocketEntity projectileEntity = new SaveFireworkRocketEntity(mobEntity.world, itemStack, mobEntity, mobEntity.getX(), mobEntity.getEyeY() - (double) 0.15f, mobEntity.getZ(), true, mobEntity.getTarget());
            projectileEntity.damage = getSkillLevel();
            projectileEntity.attrange = 5 + getSkillLevel();
            shoot(mobEntity, Objects.requireNonNull(mobEntity.getTarget()), projectileEntity, 0.0f, 1.6f);
            mobEntity.world.spawnEntity(projectileEntity);
        }
        return true;
    }
}
